package com.letv.android.client.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import com.ledim.datastatistics.LeDimAnalysisUtils;
import com.letv.ads.AdsManager;
import com.letv.ads.AdsManager.VideoCallBack;
import com.letv.android.client.LetvSDK;
import com.letv.android.client.activity.AlbumPlayActivity;
import com.letv.android.client.album.controller.AlbumPlayingHandler;
import com.letv.android.client.album.controller.AlbumWaterMarkController;
import com.letv.android.client.constant.LetvSDKConstant;
import com.letv.android.client.controller.ScreenObservable;
import com.letv.android.client.fragment.AlbumBaseControllerFragment.Style;
import com.letv.android.client.meditor.VideoControllerMeditor;
import com.letv.android.client.meditor.VideoControllerMeditor.ControllerToVideoListener;
import com.letv.android.client.view.PlayLoadLayout;
import com.letv.android.young.client.R;
import com.letv.business.flow.album.AlbumPlayFlow;
import com.letv.business.flow.album.AlbumPlayFlowObservable;
import com.letv.business.flow.album.PlayObservable;
import com.letv.business.flow.album.listener.AlbumVipTrailListener;
import com.letv.business.flow.album.listener.LoadLayoutFragmentListener;
import com.letv.business.flow.album.listener.PlayVideoFragmentListener;
import com.letv.business.flow.album.model.AlbumPlayInfo;
import com.letv.business.flow.play.PlayStatisticsUtils;
import com.letv.component.player.Interface.OnVideoViewStateChangeListener;
import com.letv.component.player.LetvMediaPlayerControl;
import com.letv.component.player.LetvVideoViewBuilder;
import com.letv.component.player.LetvVideoViewBuilder.Type;
import com.letv.component.player.utils.NativeInfos;
import com.letv.component.player.videoview.VideoViewH264m3u8;
import com.letv.core.bean.IVideo;
import com.letv.core.bean.PlayRecord;
import com.letv.core.constant.PlayConstant;
import com.letv.core.constant.PlayConstant.PlayErrCode;
import com.letv.core.utils.LetvSDKUtils;
import com.letv.core.utils.LetvUtils;
import com.letv.core.utils.LogInfo;
import com.letv.core.utils.NetworkUtils;
import com.letv.core.utils.PlayUtils;
import com.letv.core.utils.StatisticsUtils;
import com.letv.core.utils.TipUtils;
import com.letv.core.utils.UIsUtils;
import com.letv.datastatistics.DataStatistics;
import com.letv.datastatistics.util.DataConstant;
import com.letv.datastatistics.util.LetvErrorCode;
import com.letv.datastatistics.util.PageIdConstant;
import com.letv.pp.func.CdeHelper;
import com.letv.pp.func.CdeStateHelper;
import com.letv.pp.listener.CdeStateListener;
import com.letv.tracker2.agnes.VideoPlay;
import com.media.ffmpeg.FFMpegPlayer;
import com.media.ffmpeg.FFMpegPlayer.OnCacheListener;
import com.media.ffmpeg.FFMpegPlayer.OnFirstPlayLitener;
import com.media.ffmpeg.FFMpegPlayer.OnHardDecodeErrorListner;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Observable;
import java.util.Observer;

//import com.letv.component.player.fourd.LetvMediaPlayerControl4D;
//import com.letv.component.player.fourd.LetvVideoViewBuilder4D;

/**
 * 点播播放器fragment(包含videoview和mediacontroller)
 *
 * @author zhuqiao
 */
public class AlbumPlayFragment
        implements MediaPlayer.OnErrorListener, MediaPlayer.OnPreparedListener,
        MediaPlayer.OnCompletionListener, FFMpegPlayer.OnBlockListener,
        OnVideoViewStateChangeListener, OnHardDecodeErrorListner, Observer,
        ControllerToVideoListener, PlayVideoFragmentListener
{

	private static final int ON_VIDEO_START = 1;// 正片开始播放
	private static final int ON_VIDEO_PAUSE = 2;// 正片暂停
	private static final int ON_VIDEO_RESUME = 3;// 正片继续
	private static final int ON_VIDEO_COMPLATE = 4;// 当视频播放完成
	private static final int ON_VIDEO_ERROR = 5;// 当视频播放错误
	private static final int ON_VIDEO_SIZE = 6;// 当播放区域发生改变
	public static final int ON_ACTIVITY_PAUSE = 7;// 当 Activity 暂停
	public static final int ON_ACTIVITY_RESUME = 8;// 当 Activity 继续
	public static final int ON_ACTIVITY_EXIT = 9;// 当 Activity 退出

	private AlbumPlayActivity mActivity;
	private View mContentView;

	private VideoControllerMeditor mControllerMeditor;

	private LetvMediaPlayerControl mVideoView;

	private Uri mPlayUri;

	private Type mOldType;

	private String mPageId;
	/**
	 * 打开方式；用来区别播放不同的视频
	 **/
	private int mLaunchMode = PlayConstant.LAUNCH_MODE_URI;
	/**
	 * 震撼文件地址
	 **/
	private String mHaptUrl;

	/**
	 * 当前播放器状态
	 **/
	private int mCurrState;

	/**
	 * 是否该seek到0
	 **/
	private boolean mIsSeekTo0;
	/**
	 * 是否用户手动seek
	 **/
	public boolean mIsSeekByUser;

	//
	// /** 是否可以跳小窗播放 **/
	// private boolean mCanToPip = false;

	/**
	 * 水印帮助类
	 **/
	private AlbumWaterMarkController mWaterMarkController;

	/**
	 * 播放过程中handler
	 **/
	private AlbumPlayingHandler mPlayingHandler;

	/**
	 * 是否第一次进入
	 **/
	private boolean mIsFirstStart = true;
	private boolean mIsStarted = true;

	/**
	 * 离开页面时的位置.回来时定位
	 **/
	private int mCurrPosition = 0;

	// private boolean mIsScreenLock = false;

	/**
	 * 强制暂停
	 **/
	private boolean mEnforcementPause = false;

	/**
	 * 播放失败之后重试次数
	 **/
	private int mRetryTimes = 0;
	/**
	 * 如果返回true则不再重试
	 **/
	private boolean mIsBreakPlay = false;
	/**
	 * 是否是下载视频播放
	 */
	private boolean mIsDownloadVideoPlay = false;
	/**
	 * 避免00489重复上报
	 */
	private int mErrorReport;
	/**
	 * letv SDK播放器回调
	 */
	private LetvSDK.LetvSDKCallback letvSDKCallback;

	private int mMsec = 0;
	private boolean mIsLive = false;
	private boolean mIsDolby = false;
	private boolean mIsChangeStream = false;

	/**
	 * 播放显示区域
	 **/
	private Rect mRect;

	// ------- widgets --------
	private RelativeLayout mPlayerFrame;
	private ImageView mWaterMarkImageView;

	private Handler mHandler = new Handler();

	private final CdeStateHelper mCdeStateHelper = new CdeStateHelper(
	        new CdeStateListener()
	        {

		        @Override
		        public void onDownloadDurationChange(int downloadDuration)
		        {
			        // 返回值downloadDuration为当前影片已下载数据的时长，其值可能比从播放器拿到的duration值大，该情况是正常的。
			        // 若该值大于duration，则表示缓冲100%
			        LogInfo.log("zhuqiao",
			                "onDownloadDurationChange:" + downloadDuration);
			        if (mActivity.getFlow() != null)
			        {
				        AlbumPlayInfo playInfo = mActivity.getFlow()
				                .getPlayInfo();
				        playInfo.currDuration = downloadDuration;
				        if (mPlayingHandler != null
				                && mPlayingHandler.isStoped())
				        {
					        // handler暂停时，更新SecondaryProgress进度
					        int bufferPercentage = (int) (playInfo.totalTime
					                * getBufferPercentage() / 100000);
					        mControllerMeditor.updateProgress(Style.BOTH,
					                (int) (playInfo.currTime / 1000),
					                bufferPercentage);
				        }
			        }
		        }
	        }, Looper.myLooper());

	public AlbumPlayFragment(AlbumPlayActivity activity)
	{
		mActivity = activity;
		mContentView = mActivity.findViewById(R.id.play_album_bottom_frame);
		init();
		AdsManager.getInstance().setVideoCallBack(new VideoCallBack()
		{

			@Override
			public void resumeVideo()
			{
				if (mVideoView != null)
				{
					LogInfo.log("zhuqiao", "mVideoView.start()");
					mVideoView.start();
				}
			}

			@Override
			public void pauseVideo()
			{
				if (mVideoView != null)
				{
					mVideoView.pause();
				}
			}

			@Override
			public Rect getPlayerRect()
			{
				Rect rect = new Rect();
				mContentView.getGlobalVisibleRect(rect);
				return rect;
			}
		});
	}

	private void init()
	{
		letvSDKCallback = LetvSDK.getInstance().getCallBack();
		mPlayerFrame = mActivity.getViewById(R.id.play_album_videoview_frame);
		mWaterMarkImageView = mActivity.getViewById(R.id.waterMark);

		if (!TextUtils.equals("Coolpad_T2-00", LetvUtils.getModelName()))
		{
			initVideoView(false);
		}
		mWaterMarkController = new AlbumWaterMarkController(
		        mActivity.getApplicationContext(), mWaterMarkImageView,
		        mPlayerFrame);
		mPlayingHandler = new AlbumPlayingHandler(mActivity, this);
	}

	/**
	 * 从activity设置intent
	 *
	 * @param intent
	 */
	public void setIntent(Intent intent)
	{
		if (intent == null)
		{
			return;
		}

		mPageId = intent.getStringExtra(PlayConstant.PAGE_ID);
		mLaunchMode = intent.getIntExtra(PlayConstant.LAUNCH_MODE, 0);
		mHaptUrl = intent.getStringExtra(PlayConstant.HAPT_URL);
	}

	public void setControllerMeditor(VideoControllerMeditor controllerMeditor)
	{
		mControllerMeditor = controllerMeditor;
	}

	public AlbumPlayingHandler getPlayingHandler()
	{
		return mPlayingHandler;
	}

	public void onResume()
	{
		if (LetvUtils.reflectScreenState(mActivity) && !mIsFirstStart)
		{
			resume();
		}
		mIsFirstStart = false;
	}

	public void onPause()
	{
		pause();
		if (mVideoView != null)
		{
			mCurrPosition = mVideoView.getCurrentPosition();
		}
	}

	public AlbumPlayingHandler getAlbumPlayingHandler()
	{
		return mPlayingHandler;
	}

	/**
	 * 添加videoView
	 */
	private void addVideoView()
	{
		mPlayerFrame.removeAllViews();
		if (mVideoView != null)
		{
			mPlayerFrame.addView(mVideoView.getView(), new LayoutParams(
			        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		}
	}

	public void changeVideoView(Type type)
	{
		LogInfo.log("zhuqiao", "changVideoView:" + mOldType + "<<>>" + type);
		if (mOldType == type)
		{
			return;
		}

		if (mVideoView == null)
		{
			return;
		}

		mVideoView.stopPlayback();
		mVideoView = LetvVideoViewBuilder.getInstants()
		        .build(mActivity.getApplicationContext(), type);
		addVideoView();
		// setStateChangeListener((OnVideoViewStateChangeListener)
		// activity.getmPlayControllerPlayerLibs());
		LogInfo.log("zhuqiao", "---PlayNet---changeVideoView"
		        + mVideoView.getClass().getSimpleName());
		mOldType = type;
	}

	/**
	 * 初始化播放器类型 本地播放器还是系统播放器
	 */
	public void initNativeInfos()
	{
		String vf = LetvSDK.getInstance().getVideoFormat();

		if (TextUtils.equals("ios", vf))
		{
			NativeInfos.mOffLinePlay = false;
			NativeInfos.mIsLive = false;
		}
		else if (TextUtils.equals("no", vf))
		{
			NativeInfos.mOffLinePlay = true;
			NativeInfos.mIfNative3gpOrMp4 = true;
			NativeInfos.mIsLive = false;
		}
	}

	private void resume()
	{
		LogInfo.log("zhuqiao", "albumplayfragment resume");
		resumeCde();

		boolean isTryLookPause = false;// 因为试看暂停的话，resume 不执行start
		if (mActivity.getVipTrailListener() != null)
		{
			isTryLookPause = mActivity.getVipTrailListener().isTryLookPause();
		}

		if (!mIsStarted || isTryLookPause)
		{
			return;
		}

		if (mVideoView != null && mActivity.getPlayAdListener() != null
		        && mActivity.getPlayAdListener().getAdFragment() != null)
		{
			if (mActivity.getPlayAdListener().getAdFragment().isFinishAd()
			        && mActivity.getFlow() != null)
			{
				mActivity.getFlow().getPlayInfo().mVideoLoadConsumeTime = System
				        .currentTimeMillis();
				// mActivity.getFlow()
				PlayStatisticsUtils.updatePlayDataStatistics(
				        mActivity.getApplicationContext(), mActivity.getFlow(),
				        DataConstant.StaticticsVersion2Constatnt.PlayerAction.PLAY_ACTION,
				        -1, false);
				mPlayingHandler.startHandlerTime();
				if (mVideoView.getLastSeekWhenDestoryed() != 0)
				{
					LogInfo.log("zhuqiao", "###############seekto:"
					        + mVideoView.getLastSeekWhenDestoryed());
					mVideoView.seekTo(mVideoView.getLastSeekWhenDestoryed());
				}
				else
				{
					if (mCurrPosition != 0)
					{
						LogInfo.log("zhuqiao",
						        "###############seekto:" + mCurrPosition);
						mVideoView.seekTo(mCurrPosition);
					}
				}
				LogInfo.log("zhuqiao", "mVideoView.start()");
				mVideoView.start();
				if (mActivity.getLongWatchController() != null)
				{
					mActivity.getLongWatchController().cancelLongTimeWatch();
				}
				if (mActivity.getLoadListener() != null)
				{
					mActivity.getLoadListener().dismissDialog();
				}
				callAdsPlayInterface(ON_VIDEO_RESUME, false);
			}
			else if (mActivity.getFlow() != null)
			{
				mActivity.getFlow().getPlayInfo().mIsPlayingAds = true;
			}
		}

	}

	// --------------------------- 观察者回调 ---------------------------

	@Override
	public void update(Observable observable, Object data)
	{
		if (data instanceof String)
		{
			String notify = (String) data;
			if (TextUtils.equals(ScreenObservable.ON_CONFIG_CHANGE, notify))
			{
				onDirectionChange(UIsUtils.isLandscape(mActivity));
				mRect = new Rect();
				mHandler.postDelayed(new Runnable()
				{

					@Override
					public void run()
					{
						if (mContentView != null)
						{
							mContentView.getGlobalVisibleRect(mRect);
							callAdsPlayInterface(ON_VIDEO_SIZE, false);
						}
					}
				}, 1000);
			}
			else if (TextUtils.equals(PlayObservable.ON_USER_PRESENT, notify))
			{
				// mIsScreenLock = false;
				mIsStarted = true;
				resume();
			}
			else if (TextUtils.equals(AlbumPlayFlowObservable.ON_START_FETCHING,
			        notify))
			{
				if (mWaterMarkImageView != null)
				{
					mWaterMarkImageView.setVisibility(View.GONE);
				}
			}
			else if (TextUtils.equals(PlayObservable.ON_CALL_STATE_RINGING,
			        notify)
			        || TextUtils.equals(PlayObservable.ON_CALL_STATE_OFFHOOK,
			                notify))
			{
				pause();
				if (mActivity.getFlow() != null)
				{
					mActivity.getFlow().getPlayInfo().mGlsbNum += 1;
				}
				if (mPlayingHandler != null)
				{
					mPlayingHandler.onStopBack();
				}
			}
			else if (TextUtils.equals(PlayObservable.ON_CALL_STATE_IDLE,
			        notify))
			{
				if (!LetvUtils.isApplicationInBackground(mActivity))
				{
					start();
				}
			}
		}
	}

	/**
	 * 方向改变
	 *
	 * @param isLandscape
	 */
	private void onDirectionChange(boolean isLandscape)
	{
		mWaterMarkController.changeWaterMarkDirection();
		mWaterMarkImageView.getLayoutParams().width = UIsUtils
		        .getScreenWidth(mActivity) / 18;
		mWaterMarkImageView.getLayoutParams().height = mContentView
		        .getLayoutParams().height / 18;
	}

	// --------------------------- 回调 ---------------------------
	@Override
	public void onChange(int state)
	{
		mCurrState = state;
		AlbumPlayFlow flow = mActivity.getFlow();
		LoadLayoutFragmentListener loadListener = mActivity.getLoadListener();
		AlbumVipTrailListener trailListener = mActivity.getVipTrailListener();

		if (loadListener == null || flow == null || mVideoView == null)
		{
			return;
		}

		PlayRecord playRecord = flow.getPlayRecord();
		AlbumPlayInfo playInfo = flow.getPlayInfo();
		// 设置当前的状态，统计用
		playInfo.mCurrentState = mCurrState;
		if (state == VideoViewH264m3u8.STATE_PLAYING)
		{
			LogInfo.log("sguotao", "STATE_PLAYING");
			// 正片加载时间
			flow.getPlayInfo().mVideoLoadConsumeTime = System
			        .currentTimeMillis()
			        - mActivity.getFlow().getPlayInfo().mVideoLoadConsumeTime;
			long startTime = flow.getPlayInfo().mTotalConsumeTime;
			if (startTime != 0)
			{
				// 没有广告时，起播时间截止于正片第一帧
				// 有广告时，起播时间截止于广告第一帧
				// if (mActivity.getPlayAdListener().getAdFragment() != null
				// && flow.getPlayInfo().mAdsPlayFirstFrameTime == 0) {
				// flow.getPlayInfo().mAdsPlayFirstFrameTime =
				// mActivity.getPlayAdListener().getAdFragment()
				// .getAdsPlayFirstFrameTime();
				// }
				long endTime = flow.getPlayInfo().mAdCount > 0
				        ? flow.getPlayInfo().mAdsPlayFirstFrameTime
				        : System.currentTimeMillis();
				flow.getPlayInfo().mTotalConsumeTime = endTime - startTime;
				LogInfo.log("jc666",
				        "起播截止时间：" + flow.getPlayInfo().mTotalConsumeTime);
			}
			if (AlbumPlayFlow.sToPlayConsumetime != 0)
			{
				// if (playConsumetime > 3000) {
				// LogInfo.log("zhuqiao",
				// "****************cde播放大于3s****************");
				// FeedbackUtils.submitFeedbackInfo(mActivity,
				// FeedBackActivity.mContact == null ? "01234567890"
				// : FeedBackActivity.mContact, "起播慢", null, new
				// SimpleResponse<LetvBaseBean>() {
				//
				// @Override
				// public void onNetworkResponse(VolleyRequest<LetvBaseBean>
				// request, LetvBaseBean result,
				// DataHull hull, NetworkResponseState state) {
				// FeedbackUtils.submitCDEExceptionInfo(mActivity,
				// "01234567890", "起播慢", null);
				// }
				//
				// });
				// }
				LogInfo.log("zhuqiao_time",
				        "****************从得到真实地址到真正播放出视频所消耗时间"
				                + (System.currentTimeMillis()
				                        - AlbumPlayFlow.sToPlayConsumetime)
				                + "毫秒****************");
				AlbumPlayFlow.sToPlayConsumetime = 0;
			}

			PlayLoadLayout.mIsShowBlock = true;
			// 播放中
			if (loadListener.isLoadingShow() && mVideoView.isPlaying())
			{
				loadListener.finish();
			}
			mIsSeekTo0 = false;
			mIsSeekByUser = false;
			mControllerMeditor.setBlockBtnVisibile(false);
			mWaterMarkController.setNeedPauseWater(false);
			if (mWaterMarkController.startWaterMark(mVideoView))
			{
				mPlayingHandler.setHandlerTimeDelayed(1000);
			}
			flow.setPlayCallBackState(0);
			// LetvApplication.getInstance().setLoginFromHome(true);

			closePauseAd();

			if (!flow.hasInited())
			{
				// 开始播放了初始化播放记录和进度条
				mPlayingHandler.init();
				// 根据真实播放文件初始化总时长
				if (flow.getPlayRecord() != null)
				{
					flow.getPlayRecord()
					        .setTotalDuration(playInfo.totalTime / 1000);
				}
				flow.setHasInited(true);
			}

			if (mActivity.getErrorTopController() != null)
			{
				mActivity.getErrorTopController().setPlayErrorCode("", false);
			}
			if (playInfo != null)
			{
				VideoPlay videoPlay = flow.getCommonVideoPlay();
				if (!playInfo.mIsStatisticsPlay)
					LeDimAnalysisUtils.reportStartPlayEvent(videoPlay,
					        playInfo);
				playInfo.mIsStatisticsPlay = true;
			}
			// 开始刷新进度
			if (mPlayingHandler.isStoped())
			{
				mPlayingHandler.startHandlerTime();
			}

			mControllerMeditor.start(false);

			// 添加播放记录的本地看过状态
			if (playRecord != null)
			{
				playRecord.setTotalDuration(playInfo.totalTime / 1000);
			}

			saveWatchedRecord(playRecord);

			flow.setCanToPip(true);

			// letvSDKCallback.onEvent(LetvSDKConstant.EVENT_PLAY_START,
			// "start", null);
		}
		else if (mCurrState == VideoViewH264m3u8.STATE_PAUSED)
		{
			LogInfo.log("sguotao", "STATE_PAUSED");

			// 暂停播放，隐藏loading
			loadListener.finish();

			mPlayingHandler.stopHandlerTime();
			mControllerMeditor.pause();

			/* 预告片不走播放记录——— by king */
			// savePlayRecord(flow, playRecord);
			saveWatchedRecord(playRecord);

			// TODO:回调当前播放进度，总时长;
			IVideo mIVideo = getiVideo();
			if (letvSDKCallback != null)
			{
				letvSDKCallback.onEvent(LetvSDKConstant.EVENT_PLAY_PAUSE,
				        "pause", mIVideo);
			}
		}
		else if (mCurrState == VideoViewH264m3u8.STATE_ERROR)
		{
			LogInfo.log("sguotao", "STATE_ERROR");

			stopCde();
			closePauseAd();
			PlayLoadLayout.mIsShowBlock = false;

			if (flow.isDownloadFile() && !TextUtils.isEmpty(flow.getFilePath()))
			{
				// 本地视频
				loadListener.requestError(
				        mActivity.getString(R.string.commit_error_info),
				        PlayErrCode.LOCAL_VIDEO_FILE_PLAY_FAILED);
				if (mActivity.getErrorTopController() != null)
				{
					mActivity.getErrorTopController().setPlayErrorCode(
					        PlayErrCode.LOCAL_VIDEO_FILE_PLAY_FAILED, true);
				}
			}
			else if (!TextUtils.isEmpty(flow.getAlbumUrl().realUrl))
			{
				// 线上视频出错
				if (!NetworkUtils.isNetworkAvailable(mActivity))
				{
					loadListener.requestError("", "");
					if (trailListener != null)
					{
						trailListener.hide();
					}
					if (mActivity.getErrorTopController() != null)
					{
						mActivity.getErrorTopController().setPlayErrorCode("",
						        true);
					}
				}
				else
				{
					loadListener.requestError(
					        mActivity.getString(R.string.commit_error_info),
					        PlayErrCode.CURRENT_CODE_STREAM_COULD_NOT_PLAY_CORRECT);
					if (trailListener != null)
					{
						trailListener.hide();
					}
					if (mActivity.getErrorTopController() != null)
					{
						mActivity.getErrorTopController().setPlayErrorCode(
						        PlayErrCode.CURRENT_CODE_STREAM_COULD_NOT_PLAY_CORRECT,
						        true);
					}
				}
			}

			flow.setPlayCallBackState(7);
			mPlayingHandler.stopHandlerTime();

			pause();
			mVideoView.stopPlayback();
			// savePlayRecord(flow, playRecord);
			saveWatchedRecord(playRecord);

			if (letvSDKCallback != null)
			{
				letvSDKCallback.onEvent(LetvSDKConstant.EVENT_PLAY_FAILURE,
				        "failure", null);
			}

		}
		else if (mCurrState == VideoViewH264m3u8.STATE_IDLE)
		{
			LogInfo.log("sguotao", "STATE_IDLE");
			PlayLoadLayout.mIsShowBlock = false;
			closePauseAd();
			destoryCde();

			// zhuqiao trylook dlna
			if (mActivity.getFlow().getPlayCallBackState() == 0
			        && !mActivity.getLoadListener().isLoadingShow())
			{
				loadListener.loading(true);
			}

			mPlayingHandler.stopHandlerTime();
			IVideo mVideo = getiVideo();
			if (letvSDKCallback != null)
			{
				letvSDKCallback.onEvent(LetvSDKConstant.EVENT_PLAY_IDLE, "idle",
				        mVideo);
			}

		}
		else if (mCurrState == VideoViewH264m3u8.STATE_PLAYBACK_COMPLETED)
		{
			LogInfo.log("sguotao", "STATE_PLAYBACK_COMPLETED");
			PlayLoadLayout.mIsShowBlock = false;
			destoryCde();
			flow.setAlreadyPlayAd(false);
			if (mActivity.getPlayAdListener() != null)
			{
				mActivity.getPlayAdListener().closePauseAd();
			}

			// if (playRecord != null) {
			// playRecord.setPlayedDuration(-1);
			// savePlayRecord(flow, playRecord);
			// }
			saveWatchedRecord(playRecord);
			LogInfo.log("sguotao", "STATE_PLAYBACK_COMPLETED called play next");
			mPlayingHandler.playNext();
			// TODO 回调下一集信息
			if (letvSDKCallback != null)
			{
				letvSDKCallback.onEvent(LetvSDKConstant.EVENT_PLAY_NEXT, "next",
				        null);
			}

		}
		else if (mCurrState == VideoViewH264m3u8.STATE_STOPBACK)
		{
			LogInfo.log("sguotao", "STATE_STOPBACK");
			PlayLoadLayout.mIsShowBlock = false;
			if (mWaterMarkImageView != null)
			{
				mWaterMarkImageView.setVisibility(View.GONE);
			}
			flow.setAlreadyPlayAd(false);
			destoryCde();
			// 调起stopback时回调
			if (!flow.isClickShipAd())
			{
				if (flow.isFirstPlay())
				{
					// playInfo.totalConsumeTime = System.currentTimeMillis() -
					// playInfo.totalConsumeTime;
					// playInfo.loadingConsumeTime = System.currentTimeMillis()
					// - playInfo.loadingConsumeTime;
					playInfo.playVideoFirstFrameTime = System
					        .currentTimeMillis();
				}
				if (!flow.getPlayInfo().mIsStaticsEnd)
				{
					LogInfo.log("jc666", "albumplayfragment onstopback");
					mPlayingHandler.onStopBack();
				}
			}
			flow.setIsClickShipAd(false);
			AdsManager.getInstance().setFromQRCode(false);
			AdsManager.getInstance().setIsQRCodeVideoTime(0);
			// LetvApplication.getInstance().setPush(false);
			playInfo.mIsStaticsFinish = false;
			IVideo mVideo = getiVideo();
			if (letvSDKCallback != null)
			{
				letvSDKCallback.onEvent(LetvSDKConstant.EVENT_PLAY_STOP, "stop",
				        mVideo);
			}

		}
		else if (mCurrState == VideoViewH264m3u8.STATE_ENFORCEMENT)
		{
			LogInfo.log("sguotao", "STATE_ENFORCEMENT");
			if (mEnforcementPause)
			{
				// 暂停播放，隐藏loading
				loadListener.finish();
				// 停止刷新进度
				stopHandlerTime();
				mControllerMeditor.pause();
			}
			else
			{
				boolean isBufferPlayOver = mVideoView
				        .getCurrentPosition() >= mVideoView
				                .getBufferPercentage()
				                * mVideoView.getDuration() / 100;
				if (!mActivity.getFlow().isDownloadFile()
				        && !NetworkUtils.isNetworkAvailable(mActivity)
				        && isBufferPlayOver && !flow.isScanVideo())
				{
					loadListener
					        .requestError(
					                TipUtils.getTipMessage(mActivity, "100075",
					                        R.string.network_cannot_use_try_later),
					                "");
					if (trailListener != null)
					{
						trailListener.hide();
					}
					if (mActivity.getErrorTopController() != null)
					{
						mActivity.getErrorTopController().setPlayErrorCode("",
						        true);
					}
				}
				else
				{
					if (!flow.isAdFinished() || mIsSeekByUser)
					{
						loadListener.loading(true);
					}
				}
			}

			if (mVideoView.isPlaying())
			{
				if (playInfo.currTime <= 0)
				{
					mControllerMeditor.initSeekBarBeginTextView();
				}
				mControllerMeditor.setSeekbarVisibile(true);
				mControllerMeditor.hideAllControllerLayout();
			}
		}
		else if (mCurrState == VideoViewH264m3u8.STATE_PREPARED)
		{
			// 播放器准备完毕
			LogInfo.log("sguotao", "STATE_PREPARED");
			flow.addPlayInfo("播放器起播第一帧", "------------");
			mIsChangeStream = false;
			boolean isVipTrailEndShow = trailListener.isVipVideo()
			        && trailListener.isVipTrailEnd();
			if (flow.getPlayingVideo() != null && !isVipTrailEndShow)
			{
				loadListener.loadingVideo(flow.getPlayingVideo().getNameCn());
			}

			IVideo mIVideo = getiVideo();
			if (letvSDKCallback != null)
			{
				letvSDKCallback.onEvent(LetvSDKConstant.EVENT_PLAY_START,
				        "start", mIVideo);
			}
		}
	}

	private IVideo getiVideo()
	{
		AlbumPlayFlow flow = mActivity.getFlow();
		if (null == flow)
		{
			Log.e("LetvSDK", "get IVideo failed,flow is null!");
			return null;
		}
		IVideo mIVideo = new IVideo();
		mIVideo.setVid(flow.getVid());
		if (null != mActivity.getPlayingVideoBean())
		{
			mIVideo.setmTotalTime(getVideoView().getDuration());
			mIVideo.setmTitle(mActivity.getPlayingVideoBean().getNameCn());
		}
		mIVideo.setmCurrentTime(getCurrentPosition() / 1000);
		return mIVideo;
	}

	/**
	 * 保存已看过记录
	 *
	 * @param playRecord
	 */
	private void saveWatchedRecord(PlayRecord playRecord)
	{
		if (null == playRecord)
		{
			LogInfo.log("song",
			        "saveEpisodeWatchedState()  null == playRecord");
			return;
		}
		/*
		 * 保存已经看过的目录
		 * 
		 * if (mActivity.getPlayAlbumController().isEpisodeWatched()) {
		 * DBManager.getInstance().getPlayTrace()
		 * .insertPlayTraceByWatchedStatus(playRecord.getAlbumId(),
		 * playRecord.getVideoId());
		 * mActivity.getPlayAlbumController().setEpisodeWatched(false); }
		 */
	}

	/**
	 * 硬解失败回调
	 */
	@Override
	public void onBlock(FFMpegPlayer arg0, int arg1)
	{
		LogInfo.log("zhuqiao", "硬解失败回调");
		if (mActivity.getFlow() != null)
		{
			// updateBlockDataStatistics(DataConstant.StaticticsVersion2Constatnt.PlayerAction.BLOCK_ACTION,
			// blockTime,
			// "9");
			mActivity.getFlow().getPlayInfo().blockTime = 0;
		}
	}

	@Override
	public void onCompletion(MediaPlayer mp)
	{
		callAdsPlayInterface(ON_VIDEO_COMPLATE, false);
	}

	/**
	 * 准备完成回调
	 */
	@Override
	public void onPrepared(MediaPlayer mp)
	{
		LogInfo.log("zhuqiao", "onPrepared");
	}

	/**
	 * 播放错误回调
	 */
	@Override
	public boolean onError(MediaPlayer mp, int what, int extra)
	{
		// PlayConstantPlayerLibs:PlayErrorCode 257
		LogInfo.log("zhuqiao", "error:" + what + "===" + extra);
		if (what == 257)
		{
			LetvSDKUtils.reStartCde(mActivity);
		}

		AlbumPlayFlow flow = mActivity.getFlow();

		if (flow == null)
		{
			return false;
		}

		if (mIsBreakPlay)
		{
			return false;
		}

		if (mIsDownloadVideoPlay)
		{
			// 下载不能播放统计错误码
			// 统计
			DataStatistics.getInstance().sendErrorInfo(mActivity, "0", "0",
			        LetvErrorCode.VIDEO_DOWNLOAD_CAN_NOT_PLAY, null, null, null,
			        null, null, null);
			callAdsPlayInterface(ON_VIDEO_ERROR, false);
			mIsDownloadVideoPlay = false;
			return false;
		}

		String streamLevel = flow.getStreamLevel();

		StringBuilder sb = new StringBuilder();
		if (!TextUtils.isEmpty(streamLevel))
		{
			sb.append("vt=").append(streamLevel).append("&");
		}
		if (mPlayUri != null)
		{
			sb.append("playurl=").append(mPlayUri.toString());
		}

		if (what == -91)
		{
			// 播放视频超时 ，上传错误码
			DataStatistics.getInstance().sendErrorInfo(mActivity, "0", "0",
			        LetvErrorCode.VIDEO_PLAY_TIMEOUT, null, sb.toString(), null,
			        null, null, null);
		}
		else if (what == -103)
		{
			// 播放的视频文件不合法 ，上传错误码
			DataStatistics.getInstance().sendErrorInfo(mActivity, "0", "0",
			        LetvErrorCode.VIDEO_PLAY_NOT_LEGITIMATE, null,
			        sb.toString(), null, null, null, null);
		}
		else
		{
			// 播放视频文件其它错误 ，上传错误码
			mErrorReport++;
			if (mErrorReport == 1)
			{
				// 统计
				DataStatistics.getInstance().sendErrorInfo(mActivity, "0", "0",
				        LetvErrorCode.VIDEO_PLAY_OTHER_ERROR, null,
				        sb.toString(), null, null, null, null);
			}
		}
		callAdsPlayInterface(ON_VIDEO_ERROR, false);

		// 如果硬解失败之后转成软解除无网络情况
		if (NetworkUtils.isNetworkAvailable(mActivity))
		{
			mRetryTimes++;
			if (mRetryTimes == 1)
			{
				mIsBreakPlay = true;
			}
			LetvSDK.getInstance().resetVType();
			if (mPlayUri != null)
			{
				if (flow.isDownloadFile())
				{
					changeVideoView(Type.MOBILE_H264_MP4);
					startPlayLocal(mPlayUri.toString(), mMsec);
				}
				else
				{
					if (mOldType != Type.MOBILE_H264_MP4)
					{
						changeVideoView(Type.MOBILE_H264_M3U8);
						startPlayNet(mPlayUri.toString(), mIsLive, mIsDolby,
						        mMsec, mIsChangeStream);
					}
				}
			}
		}

		return false;
	}

	@Override
	public void onError(FFMpegPlayer arg0, int arg1, int arg2)
	{
		LogInfo.log("zhuqiao", "----硬解失败回调----");
	}

	/**
	 * 保存播放记录
	 */
	// private void savePlayRecord(AlbumPlayFlow flow, PlayRecord playRecord) {
	// VideoBean playingVideo = flow.getPlayingVideo();
	// if (playingVideo == null) {
	// return;
	// }
	// if (playRecord == null || TextUtils.equals("180002",
	// playRecord.getvideoTypeKey())
	// || TextUtils.equals("180002", playingVideo.getVideoTypeKey())) {
	// return;
	// }
	//
	// if (mActivity.getFlow() == null) {
	// return;
	// }
	//
	// if (playingVideo != null
	// && (playingVideo.getCid() == AlbumNew.Channel.TYPE_DOCUMENT_FILM
	// || playingVideo.getCid() == AlbumNew.Channel.TYPE_TV
	// || playingVideo.getCid() == AlbumNew.Channel.TYPE_CARTOON ||
	// playingVideo.getCid() == AlbumNew.Channel.TYPE_TVSHOW)) {
	// playRecord.setVideoNextId((int)
	// mActivity.getPlayAlbumController().getPlayRecordNextVid());
	// }
	// long playDuration = playRecord.getPlayedDuration();
	// if (PreferencesManager.getInstance().isSkip() &&
	// mActivity.getFlow().getPlayInfo().endTime != 0) {
	// if (mActivity.getFlow().getPlayInfo().endTime -
	// playRecord.getPlayedDuration() < 10) {
	// playDuration = -1;
	// }
	// } else {
	// if ((playRecord.getTotalDuration() - playRecord.getPlayedDuration() <
	// 10)) {
	// playDuration = -1;
	// }
	// }
	// playRecord.setPlayedDuration(playDuration);
	//
	// String videoTypeKey = "";
	// // if (playingVideo.getPid() > 0) {
	// // LogInfo.log("Emerson", "-----------playRecord 222222222222= " +
	// playRecord.getAlbumId());
	// // playRecord.setAlbumId((int) playingVideo.getPid());
	// // }
	//
	// videoTypeKey = !TextUtils.isEmpty(playingVideo.getVideoTypeKey()) ?
	// playingVideo.getVideoTypeKey() : playRecord
	// .getvideoTypeKey();
	// playRecord.setvideoTypeKey(videoTypeKey);
	//// LetvPlayRecordFunction.submitPlayTrace(mActivity, playRecord);
	// }

	// --------------------------- controller控制回调 ---------------------------
	@Override
	public LetvMediaPlayerControl getVideoView()
	{
		return mVideoView;
	}

	@Override
	public void onSeekFinish(int progress)
	{
		//
		// if (mActivity.getFlow() != null && progress ==
		// mActivity.getFlow().getPlayInfo().totalTime / 1000) {
		// LogInfo.log("zhuqiao", "onseekfinish called play next");
		// mPlayingHandler.playNext();
		// return;
		// }
		if (mVideoView != null)
		{
			LogInfo.log("zhuqiao", "seek to:" + progress);
			LogInfo.log("zhuqiao", "###############seekto:" + progress * 1000);
			mVideoView.seekTo(progress * 1000);
		}
		handSeekFinish(progress * 1000);
	}

	/**
	 * 开始刷新进度条
	 */
	@Override
	public void startHandlerTime()
	{
		mPlayingHandler.startHandlerTime();
	}

	/**
	 * 停止刷新进度条
	 */
	@Override
	public void stopHandlerTime()
	{
		mPlayingHandler.stopHandlerTime();
	}

	@Override
	public void buffTimeSchedule()
	{
		mPlayingHandler.buffTimeSchedule();
	}

	@Override
	public void updateProgress(boolean fromFullScreen, int progress)
	{
		mControllerMeditor.updateProgress(
		        fromFullScreen ? Style.FULL : Style.HALF, progress, -1);
	}

	/**
	 * 手动拖动之后控制显示Tip
	 *
	 * @param pos
	 */
	public void handSeekFinish(final int pos)
	{
		mPlayingHandler
		        .removeMessages(AlbumPlayingHandler.HANDLER_RECOMMEND_TIME);
		mPlayingHandler.sendMessageDelayed(
		        mPlayingHandler.obtainMessage(
		                AlbumPlayingHandler.HANDLER_RECOMMEND_TIME, pos, 0),
		        1000);
	}

	// --------------------------- handler回调 ---------------------------

	public VideoControllerMeditor getControllerMeditor()
	{
		return mControllerMeditor;
	}

	public boolean isEnforcementPause()
	{
		return mEnforcementPause;
	}

	public void closePauseAd()
	{
		if (mActivity.getPlayAdListener() != null)
		{
			mActivity.getPlayAdListener().closePauseAd();
		}
	}

	// --------------------------- PlayVideoFragmentListener回调
	// ---------------------------
	@Override
	public void initVideoView(boolean isLocal)
	{
		if (!StatisticsUtils.isFirstPlay())
		{
			StatisticsUtils.setFirstPlay(true);
			// 统计-login
			// StatisticsUtils.statisticsLogin(mActivity, 4);
			// 统计-env
			StatisticsUtils.statisticsEnv(mActivity);
		}

		// LeDimApplication com.ledim.app = LeDimApplication.getInstance();
		LetvSDK app = LetvSDK.getInstance();
		final String videoFormat = app.getVideoFormat();
		final boolean defaultStreamDecorder = app
		        .getDefaultHardStreamDecorder();

		if (isLocal)
		{
			mVideoView = LetvVideoViewBuilder.getInstants().build(
			        mActivity.getApplicationContext(), Type.MOBILE_H264_MP4);
			mOldType = Type.MOBILE_H264_MP4;
		}
		else
		{
			if (TextUtils.equals(mPageId, PageIdConstant.localPage))
			{
				mVideoView = LetvVideoViewBuilder.getInstants().build(
				        mActivity.getApplicationContext(),
				        Type.MOBILE_H264_MP4);
				mOldType = Type.MOBILE_H264_MP4;
			}
			else if (!TextUtils.equals(videoFormat, "no")
			        && mLaunchMode != PlayConstant.LAUNCH_MODE_URI
			        && mLaunchMode != PlayConstant.LAUNCH_MODE_DOWNLOAD)
			{
				// 非mp4且非缓存和扫描的视频，用m3u8
				LogInfo.log("zhuqiao", "是否用硬解：" + defaultStreamDecorder);
				if (defaultStreamDecorder)
				{
					mVideoView = LetvVideoViewBuilder.getInstants().build(
					        mActivity.getApplicationContext(),
					        Type.MOBILE_H264_M3U8_HW);
					mOldType = Type.MOBILE_H264_M3U8_HW;
				}
				else
				{
					mVideoView = LetvVideoViewBuilder.getInstants().build(
					        mActivity.getApplicationContext(),
					        Type.MOBILE_H264_M3U8);
					mOldType = Type.MOBILE_H264_M3U8;
				}
			}
			else
			{
				if (!TextUtils.isEmpty(mHaptUrl))
				{
					// LetvMediaPlayerControl4D control4D =
					// LetvVideoViewBuilder4D.getInstants().build(mActivity,
					// LetvVideoViewBuilder4D.Type.MOBILE_H264_MP4_4D);
					// control4D.setHaptUrl(mHaptUrl);
					// LogInfo.log("wlx", "= " + mHaptUrl);
					// mOldType = Type.MOBILE_H264_MP4;
					// mVideoView = control4D;
					//
					// if (!NetworkUtils.isWifi(mActivity)
					// && (NetworkUtils.getNetworkType(mActivity) !=
					// NetworkConstant.NETWORK_TYPE_NO_NET)) {
					// UIsUtils.showToast(mActivity,
					// mActivity.getString(R.string.no_wifi));
					// }
					//
					// addVideoView();
					// return;
				}
				else
				{
					// 播mp4
					mVideoView = LetvVideoViewBuilder.getInstants().build(
					        mActivity.getApplicationContext(),
					        Type.MOBILE_H264_MP4);
					mOldType = Type.MOBILE_H264_MP4;
				}
			}
		}

		// haha
		// mVideoView.setSourceType(LetvPanoramaGestureLayout.PANORAMA_ENABLE ?
		// 1 : 0);

		addVideoView();
		mVideoView.setOnCacheListener(new OnCacheListener()
		{
			private long startTime = 0;

			@Override
			public void onCache(FFMpegPlayer arg0, int arg1, int arg2,
			        long arg3)
			{
				if (arg1 == FFMpegPlayer.MEDIA_CACHE_START)
				{ // 卡顿开始
					LogInfo.LogStatistics("点播-卡顿开始");
					startTime = System.currentTimeMillis();
					// mActivity.getFlow().updateBlockDataStatistics(true, 0);
					PlayStatisticsUtils.updateBlockDataStatistics(
					        mActivity.getApplicationContext(),
					        mActivity.getFlow(), true, 0);
				}
				else if (arg1 == FFMpegPlayer.MEDIA_CACHE_END)
				{ // 卡顿结束
					LogInfo.LogStatistics("点播-卡顿结束");
					// mActivity.getFlow()
					PlayStatisticsUtils.updateBlockDataStatistics(
					        mActivity.getApplicationContext(),
					        mActivity.getFlow(), false,
					        System.currentTimeMillis() - startTime);
				}
			}
		});
		mVideoView.setOnFirstPlayListener(new OnFirstPlayLitener()
		{

			@Override
			public void onFirstPlay(FFMpegPlayer arg0)
			{
				LogInfo.log("zhuqiao", "onFirstPlay");

				if (mActivity.getFlow() != null)
				{
					mActivity.getFlow().addPlayInfo("播放出第一帧", "");
					// mActivity.getFlow().getPlayInfo().mVideoLoadConsumeTime =
					// System.currentTimeMillis()
					// -
					// mActivity.getFlow().getPlayInfo().mVideoLoadConsumeTime;
					// // 没有广告时，起播时间截止于正片第一帧
					// if (!mActivity.getFlow().isHasAd()) {
					// mActivity.getFlow().getPlayInfo().mTotalConsumeTime =
					// System.currentTimeMillis()
					// - mActivity.getFlow().getPlayInfo().mTotalConsumeTime;
					// }
				}

				if (mActivity.getLoadListener() != null
				        && mActivity.getLoadListener().isLoadingShow())
				{
					mActivity.getLoadListener().finish();
				}
			}
		});
	}

	/**
	 * 开始播放
	 */
	@Override
	public void start()
	{
		if (mVideoView != null)
		{
			LogInfo.log("zhuqiao", "mVideoView.start()");
			mPlayingHandler.startHandlerTime();
			mVideoView.start();
			mWaterMarkController.setNeedPauseWater(false);
		}

		callAdsPlayInterface(ON_VIDEO_RESUME, true);
	}

	/**
	 * 暂停播放
	 */
	@Override
	public void pause()
	{
		if (mVideoView != null)
		{
			mVideoView.pause();
			mWaterMarkController.setNeedPauseWater(true);
		}

		callAdsPlayInterface(ON_VIDEO_PAUSE, true);
	}

	@Override
	public void stopPlayback()
	{
		if (mVideoView != null)
		{
			mVideoView.stopPlayback();
		}
	}

	@Override
	public boolean isPlaying()
	{
		return mVideoView != null ? mVideoView.isPlaying() : false;
	}

	@Override
	public boolean isPaused()
	{
		return mVideoView != null ? mVideoView.isPaused() : false;
	}

	@Override
	public void resetPlayFlag()
	{
		mRetryTimes = 0;
		mIsBreakPlay = false;
	}

	@Override
	public void setEnforcementPause(boolean pause)
	{
		if (mVideoView != null)
		{
			mEnforcementPause = pause;
			mVideoView.setEnforcementPause(pause);
		}
	}

	@Override
	public void setEnforcementWait(boolean wait)
	{
		if (mVideoView != null)
		{
			mVideoView.setEnforcementWait(wait);
		}
	}

	@Override
	public void setIsSeekTo0(boolean seek)
	{
		mIsSeekTo0 = seek;
	}

	@Override
	public boolean isSeekTo0()
	{
		return mIsSeekTo0;
	}

	@Override
	public void seekTo(long position)
	{
		if (mVideoView != null)
		{
			LogInfo.log("zhuqiao", "###############seekto:" + position);
			mVideoView.seekTo((int) position);
		}
	}

	@Override
	public long getCurrentPosition()
	{
		return mVideoView != null ? mVideoView.getCurrentPosition() : 0;
	}

	@Override
	public int getBufferPercentage()
	{
		return mPlayingHandler.getBufferPercentage();
	}

	@Override
	public void startPlayLocal(String uri, int msec)
	{
		if (TextUtils.isEmpty(uri) || mVideoView == null || mActivity == null
		        || mActivity.getFlow() == null)
		{
			return;
		}

		mActivity.getFlow().addPlayInfo("创建本地播放器,播放地址", uri);

		if (AlbumPlayFlow.sRequestRealUrlConsumetime != 0)
		{
			long requestRealUrlConsumetime = System.currentTimeMillis()
			        - AlbumPlayFlow.sRequestRealUrlConsumetime;
			LogInfo.log("zhuqiao", "****************本地视频...从其启动流程到得到真实地址所消耗时间"
			        + requestRealUrlConsumetime + "毫秒****************");
			AlbumPlayFlow.sRequestRealUrlConsumetime = 0;
			AlbumPlayFlow.sToPlayConsumetime = System.currentTimeMillis();
		}

		mActivity.getFlow().getPlayInfo().mVideoLoadConsumeTime = System
		        .currentTimeMillis();

		LogInfo.log("zhuqiao", "播放本地视频：" + uri);
		mIsDownloadVideoPlay = true;
		NativeInfos.mIsLive = false;
		NativeInfos.mOffLinePlay = true;
		NativeInfos.doWithNativePlayUrl(uri);
		mPlayUri = Uri.parse(uri);
		mMsec = msec;
		changeVideoView(Type.MOBILE_H264_MP4);
		mVideoView.setVideoPlayUrl(PlayUtils.getPlayUrl(uri, mOldType,
		        mActivity.getFlow().getPlayLevel()));
		mVideoView.setOnErrorListener(this);
		mVideoView.setOnCompletionListener(this);
		mVideoView.setOnPreparedListener(this);
		mVideoView.setVideoViewStateChangeListener(this);
		mVideoView.getView().requestFocus();
		if (msec > 0)
		{
			mVideoView.seekTo(msec);
		}
		LogInfo.log("zhuqiao", "mVideoView.start()");
		mVideoView.start();
	}

	@Override
	public void hideRecommendTip()
	{
		if (mActivity.getRecommendController() != null)
		{
			mActivity.getRecommendController().hideRecommendTipView();
		}
	}

	/**
	 * 广告接口回调播放各种状态
	 *
	 * @param whichStatus
	 * @param isHand
	 */
	public void callAdsPlayInterface(int whichStatus, boolean isHand)
	{
		AlbumPlayFlow flow = mActivity.getFlow();
		if (flow == null)
		{
			return;
		}

		try
		{
			if (flow.mIVideoStatusInformer != null)
			{
				switch (whichStatus)
				{
					case ON_VIDEO_START:
						flow.mIVideoStatusInformer.OnVideoStart(isHand);// 正片开始播放
						LogInfo.log("zhuqiao",
						        "~~~~~~~~~~~~~~~~~~~~OnVideoStart~~~~~~~~~~~~~~~~~~~");
						break;
					case ON_VIDEO_PAUSE:
						flow.mIVideoStatusInformer.OnVideoPause(isHand);// 正片暂停
						// 是否由用户主动触发
						LogInfo.log("zhuqiao",
						        "~~~~~~~~~~~~~~~~~~~OnVideoPause~~~~~~~~~~~~~~~~~~~");
						break;
					case ON_VIDEO_RESUME:
						flow.mIVideoStatusInformer.OnVideoResume(isHand);// 正片继续
						// 是否由用户主动触发
						LogInfo.log("zhuqiao",
						        "~~~~~~~~~~~~~~~~~~~OnVideoResume~~~~~~~~~~~~~~~~~~~");
						break;
					case ON_VIDEO_COMPLATE:
						flow.mIVideoStatusInformer.OnVideoComplate();// 当视频播放完成
						LogInfo.log("zhuqiao",
						        "~~~~~~~~~~~~~~~~~~~OnVideoComplate~~~~~~~~~~~~~~~~~~~");
						break;
					case ON_VIDEO_ERROR:
						flow.mIVideoStatusInformer.onVideoError();// 当视频播放错误
						LogInfo.log("zhuqiao",
						        "~~~~~~~~~~~~~~~~~~~onVideoError~~~~~~~~~~~~~~~~~~~");
						break;
					case ON_VIDEO_SIZE:
						flow.mIVideoStatusInformer.OnVideoResize(mRect);
						LogInfo.log("zhuqiao",
						        "~~~~~~~~~~~~~~~~~~~OnVideoResize~~~~~~~~~~~~~~~~~~~");
						break;
					case ON_ACTIVITY_PAUSE:
						flow.mIVideoStatusInformer.OnActivityPause();
						LogInfo.log("zhuqiao",
						        "~~~~~~~~~~~~~~~~~~~OnActivityPause~~~~~~~~~~~~~~~~~~~");
						break;
					case ON_ACTIVITY_RESUME:
						flow.mIVideoStatusInformer.OnActivityResume();
						LogInfo.log("zhuqiao",
						        "~~~~~~~~~~~~~~~~~~~OnActivityResume~~~~~~~~~~~~~~~~~~~");
						break;
					case ON_ACTIVITY_EXIT:
						flow.mIVideoStatusInformer.OnActivityExit();
						LogInfo.log("zhuqiao",
						        "~~~~~~~~~~~~~~~~~~~OnActivityExit~~~~~~~~~~~~~~~~~~~");
						break;
				}
			}
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	// @Override
	// public PlayRecord getPoint(int pid, int vid, boolean isDownload) {
	// return LetvPlayRecordFunction.getPoint(pid, vid, isDownload);
	//
	// }

	@Override
	public void startPlayNet(String url, boolean isLive, boolean isDolby,
	        int msec, boolean isChangeStream)
	{
		if (TextUtils.isEmpty(url) || mVideoView == null || mActivity == null
		        || mActivity.getFlow() == null)
		{
			return;
		}

		mActivity.getFlow().addPlayInfo("创建播放器,播放地址", url);
		LogInfo.log("sguotao", "创建播放器,播放地址:" + url);

		mActivity.getFlow().getPlayInfo().mVideoLoadConsumeTime = System
		        .currentTimeMillis();

		LogInfo.log("sguotao",
		        "****************广告所消耗时间"
		                + (System.currentTimeMillis()
		                        - AlbumPlayFlow.sRequestAdsConsumetime)
		                + "毫秒****************");
		LogInfo.log("sguotao",
		        "****************从其启动流程到得到真实地址所消耗时间"
		                + (System.currentTimeMillis()
		                        - AlbumPlayFlow.sRequestRealUrlConsumetime)
		                + "毫秒****************");
		AlbumPlayFlow.sToPlayConsumetime = System.currentTimeMillis();

		boolean isHardDecorder = LetvSDK.getInstance()
		        .getDefaultHardStreamDecorder();
		// 如果支持m3u8则切换到m3u8播放器不然还是mp4
		if (("ios".equals(LetvSDK.getInstance().getVideoFormat())) && !isDolby)
		{
			if (isHardDecorder)
			{
				changeVideoView(Type.MOBILE_H264_M3U8_HW);
			}
			else
			{
				changeVideoView(Type.MOBILE_H264_M3U8);
			}
		}
		else
		{
			changeVideoView(Type.MOBILE_H264_MP4);
		}

		NativeInfos.mOffLinePlay = false;
		initNativeInfos();
		NativeInfos.mIsLive = false;

		if (isDolby)
		{
			NativeInfos.mOffLinePlay = true;
			NativeInfos.mIfNative3gpOrMp4 = true;
			NativeInfos.mIsLive = false;
		}

		mPlayUri = Uri.parse(url);
		mMsec = msec;
		mIsDolby = isDolby;
		mIsLive = isLive;

		mVideoView.setVideoPlayUrl(PlayUtils.getPlayUrl(url, mOldType,
		        mActivity.getFlow().getPlayLevel()));
		startCde(url);
		mVideoView.setOnErrorListener(this);
		mVideoView.setOnCompletionListener(this);
		mVideoView.setOnPreparedListener(this);
		mVideoView.setVideoViewStateChangeListener(this);
		mVideoView.setOnHardDecodeErrorListener(this);
		mVideoView.getView().requestFocus();
		if (msec > 0)
		{
			LogInfo.log("sguotao", "###############seekto:" + msec);
			mVideoView.seekTo(msec);
		}
		LogInfo.log("sguotao", "mVideoView.start()");
		mVideoView.start();

		if (isChangeStream)
		{
			if (mPlayingHandler != null)
			{
				mPlayingHandler.onStopBack();
			}
		}
		mIsChangeStream = isChangeStream;
		if (isChangeStream)
		{
			// 如果是切换码流，通知广告resume
			callAdsPlayInterface(ON_VIDEO_RESUME, false);
		}
		else
		{
			callAdsPlayInterface(ON_VIDEO_START, false);
		}
	}

	@Override
	public void startCde(String url)
	{
		if (mCdeStateHelper != null)
		{
			mCdeStateHelper.start(url);
		}
	}

	@Override
	public void stopCde()
	{
		if (mCdeStateHelper != null)
		{
			mCdeStateHelper.stop();
		}
	}

	@Override
	public void handlerFloatBall(String pageId, int cid)
	{
		LogInfo.log("Emerson",
		        "-------------pageId = " + pageId + "----cid = " + cid);
		// if (mActivity.getFloatBallUI() != null) {
		// mActivity.getFloatBallUI().showFloat(pageId, cid + "");
		// }
	}

	@Override
	public void startOverall()
	{
		if (mActivity.getController() != null)
		{
			mActivity.getController().start();
		}
	}

	@Override
	public void finishPlayer(boolean isJumpToPip)
	{
		if (mActivity.getController() != null)
		{
			mActivity.getController().finishPlayer(isJumpToPip);
		}
	}

	public void pauseCde()
	{
		// CdeHelper cdeHelper = LetvApplication.getInstance().getCdeHelper();
		CdeHelper cdeHelper = LetvSDKUtils.getCdeHelper(mActivity);
		if (mPlayUri != null && mActivity.getFlow() != null && cdeHelper != null
		        && LetvSDK.getInstance().isCdeStarting())
		{
			LogInfo.log("sguotao", "暂停cde");
			cdeHelper.pausePlay(mPlayUri.toString());
		}
	}

	public void resumeCde()
	{
		// CdeHelper cdeHelper = LetvApplication.getInstance().getCdeHelper();
		CdeHelper cdeHelper = LetvSDKUtils.getCdeHelper(mActivity);
		if (mPlayUri != null && mActivity.getFlow() != null && cdeHelper != null
		        && LetvSDK.getInstance().isCdeStarting())
		{
			LogInfo.log("sguotao", "恢复cde:" + mPlayUri.toString());
			cdeHelper.resumePlay(mPlayUri.toString());
		}
	}

	public void destoryCde()
	{
		AlbumPlayFlow flow = mActivity.getFlow();
		if (flow == null)
		{
			return;
		}

		CdeHelper cde = LetvSDKUtils.getCdeHelper(mActivity);
		if (flow.isP2pMode() && null != cde)
		{
			if (flow.getAlbumUrl().p2pPlayer != null)
			{
				final String stopUrl = cde
				        .getStopUrl(flow.getAlbumUrl().linkShellUrl);
				stopCde();
				new Thread()
				{

					@Override
					public void run()
					{
						LogInfo.log("zhuqiao", "关闭cde:");
						try
						{
							URL url = new URL(stopUrl);
							HttpURLConnection conn = (HttpURLConnection) url
							        .openConnection();
							int response = conn.getResponseCode();
							conn.getInputStream();
							if (response == 200)
							{
								LogInfo.log("zhuqiao", "关闭cde-------");
							}
						} catch (Exception e)
						{
							e.printStackTrace();
						}
					}

				}.start();
			}
		}
	}

	public int getCurrState()
	{
		return mCurrState;
	}

	public void onDestroy()
	{
		if (mVideoView != null)
		{
			mVideoView.setVideoViewStateChangeListener(null);
			mVideoView.stopPlayback();
		}
		else
		{
			destoryCde();
		}
	}

}
